//
//  WHRProductCommentCell.m
//  仿造淘宝商品详情页
//
//  Created by 天质弘耕 on 2018/11/23.
//  Copyright © 2018 yixiang. All rights reserved.
//
//商品评论

#import "WHRProductCommentCell.h"
#import "CommentImageCollectionViewCell.h"
#import "UIImageView+WebCache.h"


@interface WHRProductCommentCell ()<UICollectionViewDelegate, UICollectionViewDataSource>


@end

@implementation WHRProductCommentCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self creatView];
        [self setlayout];
    }
    return self;
}


- (void)creatView {
    
    /** 用户图像 */
    _userImageView = [[UIImageView alloc]init];
     _userImageView.contentMode =UIViewContentModeScaleAspectFill;
     _userImageView.clipsToBounds=YES;//
    _userImageView.cornerRadius = 25;
    _userImageView.image = [UIImage imageNamed:@"xy.jpg"];
    [self.contentView addSubview:self.userImageView];
    
    /** 用户名字 */
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.font = FONT_FOURTEEN;
    self.nameLabel.textColor = COLOR_Gray;
    [self.contentView addSubview:self.nameLabel];
    
    /** 评论时间 */
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.font = FONT_FOURTEEN;
    self.timeLabel.textColor = COLOR_Gray;
    [self.contentView addSubview:self.timeLabel];
    
    /** 评论内容 */
    self.commentLabel = [[UILabel alloc]init];
    self.commentLabel.font = FONT_FOURTEEN;
    self.commentLabel.textColor = COLOR_Gray;
    [self.contentView addSubview:self.commentLabel];
    
    /** 星星 */
    self.starAppraiseView = [[ ZXSampleStarAppraiseView alloc]init];
    [self.contentView addSubview:self.starAppraiseView];
    
    /** 显示图片 */
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 40) / 3.f, (SCREEN_WIDTH - 40) / 3.f);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[CommentImageCollectionViewCell class] forCellWithReuseIdentifier:@"CommentImageCollectionViewCell"];
    [self.contentView addSubview:_collectionView];
  
}

- (void)setlayout {
    
    self.userImageView.sd_layout
    .leftSpaceToView(self.contentView, 10)
    .topSpaceToView(self.contentView, 10)
    .widthIs(50)
    .heightIs(50);
    
    self.nameLabel.sd_layout
    .leftSpaceToView(self.userImageView, 10)
    .topSpaceToView(self.contentView, 10)
    .widthIs(SCREEN_WIDTH-200)
    .autoHeightRatio(0)
    .minHeightIs(40);
    
    
    self.starAppraiseView.sd_layout
    .rightSpaceToView(self.contentView, 10)
    .topSpaceToView(self.contentView, 10)
    .widthIs(100)
    .heightIs(20);
    
    self.timeLabel.sd_layout
    .leftSpaceToView(self.userImageView, 10)
    .topSpaceToView(self.nameLabel, 10)
    .widthIs(SCREEN_WIDTH-80)
    .heightIs(20);
    
    self.commentLabel.sd_layout
    .leftSpaceToView(self.contentView, 10)
    .topSpaceToView(self.timeLabel, 10)
    .widthIs(SCREEN_WIDTH)
    .autoHeightRatio(0)
    .minWidthIs(40);
    
    
    CGFloat hegit = 0;
    if (_model.commentImages.count % 3 == 0) {
        hegit = (_model.commentImages.count / 3) * ((SCREEN_WIDTH - 40) / 3.f + 10) + 10;
    } else {
        hegit = (_model.commentImages.count / 3 + 1) * ((SCREEN_WIDTH - 40) / 3.f + 10) + 10;
    }
    
    _collectionView.sd_layout
    .topSpaceToView(_commentLabel, 10)
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(hegit);
    
    [self setupAutoHeightWithBottomView:_collectionView bottomMargin:10];
    
    

}





- (void)setModel:(WHRTGCommentModel *)model {
    
      _model = model;
    [_userImageView  sd_setImageWithURL:[NSURL URLWithString:_model.commentUserFaceIcon] placeholderImage:[UIImage imageNamed:@""]];
    _nameLabel.text = _model.commentUserNickName;
    _timeLabel.text = _model.commentDatetime;
    _commentLabel.text = _model.comment;
    _starAppraiseView.score = [_model.multipleLevel integerValue];
    [self.collectionView reloadData];
    [self setlayout];
    
    
}



#pragma mark - UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _model.commentImages.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommentImageCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor cyanColor];
    cell.imageUrlStr = _model.commentImages[indexPath.row];
    
    return cell;
}






@end
